package DAO;

import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;

import DBUntil.DbHelper;
import each_class.SongClass;

public class SongDOA {
	private static DbHelper db = new DbHelper();

	//
	public static boolean judgeExist(String id) throws IOException {
		String sql = "select song_id from song where song_id=?";
		Map<String, Object> map = db.findsingle(sql, id);
		if (null == map) {
			return true;
		}
		return false;
	}

	// 添加
	public static int addSong(SongClass songClass) throws Exception {
		if (songClass.getSong_id() == null) {
			return 0;
		}
		if (judgeExist(songClass.getSong_id())) {
			String song_id = songClass.getSong_id();
			String song_name = songClass.getSong_name();
			String singername;
			Singers_DAO sd = new Singers_DAO();
			StringBuilder stringBuilder = new StringBuilder();
			if (songClass.getSingerid() != null) {
				for (Entry<String, String> entry : songClass.getSingers().entrySet()) {

					sd.addSingers(entry.getKey(), entry.getValue());
					stringBuilder.append(entry.getKey()).append('/');
				}
			}
			singername = stringBuilder.toString();
			String song_album_id = songClass.getAlbum_id();
			String song_album_name = songClass.getAlbum_name();
			String song_time = songClass.getSong_time();
			String image_Url = songClass.getSong_image_url();
			String song_cachingpath = songClass.getCachingpath();
			String song_dowpath = songClass.getDowpath();

			String sql = "insert into song values(?,?,?,?,?,?,?,?,?)";
			return db.update(sql, song_id, song_name, singername, song_album_id, song_album_name, song_time, image_Url,
					song_cachingpath, song_dowpath);
		} else {
			return 0;
		}

	}

	// 查找
	public static Map<String, Object> find(String id) throws IOException {
		String sql = "select song_id, song_name, singer_id, song_album_id, song_album_name, song_time, image_Url,"
				+ "song_cachingpath, song_dowpath from song where song_id=?";
		return db.findsingle(sql, id);
	}

	// 删除
	public static int disSong(String song_id) throws Exception {
		if(!judgeExist(song_id)) {
				String sql = "delete from song where song_id=?";
		return db.update(sql, song_id);
		}
	return 0;
	}

}